/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmAddUser.java
 *
 * Created on 04/09/2012, 15:47:47
 */
package br.com.petshop.dao.frames;

import br.com.petshop.dao.ProdutoDAO;
import br.com.petshop.entidades.Produto;
import java.sql.SQLException;
import java.text.DecimalFormat;
import javax.swing.JOptionPane;

/**
 *
 * @author Alexandre
 */
public class frmAdicionarProdutos extends javax.swing.JDialog {
    public final ProdutoDAO prodDAO;
    public final Produto produto = new Produto();//aqui a instancia do Produto vira atributo da classe.
    /** Creates new form frmAddUser */
    public frmAdicionarProdutos(java.awt.Frame parent, boolean modal,ProdutoDAO prodDAO) {
        super(parent, modal);
        this.prodDAO = prodDAO;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jl_Nome = new javax.swing.JLabel();
        jl_Preco = new javax.swing.JLabel();
        jl_Estoque = new javax.swing.JLabel();
        tf_Nome = new javax.swing.JTextField();
        tf_Preco = new javax.swing.JTextField();
        tf_Estoqe = new javax.swing.JTextField();
        jb_Add = new javax.swing.JButton();
        jb_Canc = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Adicionar Produto");
        setIconImage(null);
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jl_Nome.setText("Nome: ");

        jl_Preco.setText("Preco: ");

        jl_Estoque.setText("Estoque: ");

        tf_Estoqe.setToolTipText("Se não inserir nenhum valor o estoque conterá 10 produtos");

        jb_Add.setText("Adicionar");
        jb_Add.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_AddActionPerformed(evt);
            }
        });

        jb_Canc.setText("Cancelar");
        jb_Canc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_CancActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jl_Nome)
                            .addComponent(jl_Estoque))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(tf_Estoqe)
                            .addComponent(tf_Preco)
                            .addComponent(tf_Nome, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE))
                        .addContainerGap(110, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jb_Add)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jb_Canc)
                        .addGap(128, 128, 128))
                    .addComponent(jl_Preco)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_Nome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jl_Nome))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jl_Preco, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tf_Preco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(13, 13, 13)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_Estoqe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jl_Estoque))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jb_Add)
                    .addComponent(jb_Canc))
                .addContainerGap(37, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(25, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-410)/2, (screenSize.height-261)/2, 410, 261);
    }// </editor-fold>//GEN-END:initComponents

    private void jb_CancActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_CancActionPerformed
        setVisible(false);
    }//GEN-LAST:event_jb_CancActionPerformed

    private void jb_AddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_AddActionPerformed
        //valida se os campos estao sendo preenchidos pelo usuario
        if(tf_Nome.getText().equals("")){
            JOptionPane.showMessageDialog(this, "Por Favor, Insira Um Produto!");
            return;
        }
       if(tf_Preco.getText().equals("")){
           JOptionPane.showMessageDialog(this, "Por Favor, Informe o Preco do Produto!");
           return;
       }
        int result = JOptionPane.showConfirmDialog(this,"Confirma?","Novo Produto?",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
        if(result != JOptionPane.YES_OPTION)return;//constante 0 confirma, 2 cancela      
        //Colocar este trecho dentro de um bloco try por causa das conversoes Float e Integer       
        
        try{
        produto.setNome(tf_Nome.getText());
        /*DecimalFormat df = new DecimalFormat("#,00");
        float preco = Float.parseFloat(tf_Preco.getText());        
        df.format(preco);*/
        produto.setPreco(Float.parseFloat(tf_Preco.getText()));
        if(!tf_Estoqe.getText().equals("")){
        produto.setEstoque(Integer.parseInt(tf_Estoqe.getText()));
        }else{
            produto.setEstoque(1);//definido no inicio do projeto que 10 deve ser o valor mínimo de estoque,
            //mas falta definir se ao cadastrar só pode ser feito a partir de 10, neste momento apenas está sendo
            //tratada a exceção NumberFormatException
        }
        prodDAO.adicionarProduto(produto);
        } catch (SQLException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this,"Erro Ao Adicionar Usuario!" + ex+ JOptionPane.ERROR_MESSAGE);
        }
        catch(NumberFormatException n){
            JOptionPane.showMessageDialog(this, "Impossivel Converter" + n); 
            return;
        }        
      
        setVisible(false);       
    }//GEN-LAST:event_jb_AddActionPerformed

    /**
     * @param args the command line arguments
     
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                frmAdicionarProdutos dialog = new frmAdicionarProdutos(new javax.swing.JFrame(), true,new ProdutoDAO());
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }*/
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton jb_Add;
    private javax.swing.JButton jb_Canc;
    private javax.swing.JLabel jl_Estoque;
    private javax.swing.JLabel jl_Nome;
    private javax.swing.JLabel jl_Preco;
    private javax.swing.JTextField tf_Estoqe;
    private javax.swing.JTextField tf_Nome;
    private javax.swing.JTextField tf_Preco;
    // End of variables declaration//GEN-END:variables
}
